package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.ProductsMapper;
import com.pojo.TbProducts;
import com.pojo.TbUsers;
import com.service.ProductsService;
import com.util.JSONResult;
import com.util.UploadTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, TbProducts> implements ProductsService {
    @Autowired
    JSONResult jsonResult;
    @Autowired
    ProductsMapper productsMapper;
    @Autowired
    UploadTools uploadTools;

    @Override
    public JSONResult findAllProductByCode(int page, int limit, String code) {
        Page<TbProducts> tbpage = new Page<>(page,limit);
        if (code!=null&&code!=""){
            QueryWrapper<TbProducts> tbProducts = new QueryWrapper<>();
            tbProducts.eq("product_code",code);
            Page<TbProducts> page1 = this.page(tbpage,tbProducts);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }
        Page<TbProducts> page1 = this.page(tbpage);
        return jsonResult.success(201,"code不能为空",null,null);
    }


    @Override
    public JSONResult findAllProduct(int page, int limit) {
        Page<TbProducts> tbpage = new Page<>(page,limit);
        QueryWrapper<TbProducts> tbProducts = new QueryWrapper<>();
        Page<TbProducts> page1 = this.page(tbpage,tbProducts);
        return jsonResult.success(200,"查询成功",page1,page1.getTotal());
    }

    @Override
    public JSONResult findAllProductByTime(int page, int limit, String starttime, String endtime) throws ParseException {
        Page<TbProducts> tbpage = new Page<>(page,limit);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date start = format.parse(starttime);
        Date end = format.parse(endtime);
        QueryWrapper<TbProducts> tbProducts = new QueryWrapper<>();
        QueryWrapper<TbProducts> product = tbProducts.between("product_creattime", start, end);
//        List<TbProducts> tbProducts1 = productsMapper.selectList(tbProducts);
        if (product!=null){
            Page<TbProducts> page1 = this.page(tbpage,product);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }
        Page<TbProducts> page1 = this.page(tbpage,product);
        return jsonResult.success(200,"查询成功",page1,page1.getTotal());
    }

    @Override
    public JSONResult addProduct(TbProducts products) {
        boolean b = this.save(products);
        if (b){
            return jsonResult.success(200,"添加成功",null,null);
        }
        return jsonResult.failure(201,"添加失败",null,null);
    }

    @Override
    public JSONResult deleteProduct(int pid) {
        boolean b = this.removeById(pid);
        if (b){
            return jsonResult.success(200,"删除成功",null,null);
        }
        return jsonResult.failure(201,"删除失败",null,null);
    }

    @Override
    public JSONResult updateProduct(TbProducts products) {
        boolean b = this.saveOrUpdate(products);
        if (b){
            return jsonResult.success(200,"修改成功",null,null);
        }
        return jsonResult.failure(201,"修改失败",null,null);
    }

    @Override
    public JSONResult findProductById(int pid) {
        QueryWrapper<TbProducts> tbProducts = new QueryWrapper<>();
        tbProducts.eq("product_id",pid);
        TbProducts products = this.getOne(tbProducts);
        if (products!=null){
            return jsonResult.success(200,"查询成功",products,null);
        }
        return jsonResult.failure(201,"查询失败",null,null);
    }

    @Override
    public JSONResult updateState(int pid,int state) {
        if (state==0){
            int i = productsMapper.updateState1(pid);
            if (i==1){
                return jsonResult.success(200,"上架成功",null,null);
            }
            return jsonResult.failure(201,"上架失败",null,null);
        }else if (state==1){
            int i = productsMapper.updateState0(pid);
            if (i==1){
                return jsonResult.success(200,"下架成功",null,null);
            }
            return jsonResult.failure(201,"下架失败",null,null);
        }
        return jsonResult.failure(202,"修改失败",null,null);
    }

    @Override
    public JSONResult upload(MultipartFile file) {
        try {
            return jsonResult.success(200, "上传成功", uploadTools.upload(file), null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonResult.failure(201,"上传失败",null,null);
    }

    @Override
    public JSONResult findProductByAttention(int page, int limit, int attention) {
        Page<TbProducts> tbpage = new Page<>(page,limit);
        if (attention==-1){
            return jsonResult.failure(201,"状态码为空,查询失败",null,null);
        }
        if (attention==0){
            QueryWrapper<TbProducts> tbProducts = new QueryWrapper<>();
            tbProducts.eq("product_attention",attention);
            Page<TbProducts> page1 = this.page(tbpage,tbProducts);
            return jsonResult.success(200,"未关注查询成功",page1,page1.getTotal());
        }else if (attention==1){
            QueryWrapper<TbProducts> tbProducts = new QueryWrapper<>();
            tbProducts.eq("product_attention",attention);
            Page<TbProducts> page1 = this.page(tbpage,tbProducts);
            return jsonResult.success(200,"已关注查询成功",page1,page1.getTotal());
        }
        Page<TbProducts> page1 = this.page(tbpage);
        return jsonResult.success(201,"状态码错误,查询失败",null,null);
    }

    @Override
    public JSONResult updateAttention(int pid, int attention) {
        if (attention==0){
            int i = productsMapper.updateAttention1(pid);
            if (i==1){
                return jsonResult.success(200,"关注成功",null,null);
            }
            return jsonResult.failure(201,"关注失败",null,null);
        }else if (attention==1){
            int i = productsMapper.updateAttention0(pid);
            if (i==1){
                return jsonResult.success(200,"取消关注",null,null);
            }
            return jsonResult.failure(201,"取消关注失败",null,null);
        }
        return jsonResult.failure(202,"修改失败",null,null);
    }
}
